package br.gendi.rcp;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import br.gendi.core.Conectividade;
import br.gendi.dialog.Visualizador;

public class View extends ViewPart {

	private Label label = null;
	private Text text = null;
	private Button button = null;
	private Label label1 = null;
	private Text textArea = null;
	private Button button1 = null;
	private Button button2 = null;
	private Visualizador v = null;

	public static final String ID = "br.gendi.rcp.view";

	public void createPartControl(final Composite parent) {
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.BEGINNING;
		gridData6.verticalAlignment = GridData.CENTER;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.BEGINNING;
		gridData5.verticalAlignment = GridData.BEGINNING;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.verticalAlignment = GridData.BEGINNING;
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.BEGINNING;
		GridData gridData2 = new GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		gridData2.horizontalAlignment = GridData.FILL;
		GridData gridData1 = new GridData();
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.verticalAlignment = GridData.FILL;
		gridData1.grabExcessVerticalSpace = true;
		gridData1.horizontalAlignment = GridData.FILL;
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.CENTER;
		gridData.horizontalSpan = 2;
		gridData.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		label = new Label(parent, SWT.NONE);
		label.setText("Nova base genômica:");
		label.setLayoutData(gridData6);
		text = new Text(parent, SWT.BORDER);
		text.setLayoutData(gridData2);
		button = new Button(parent, SWT.NONE);
		button.setText("Adicionar");
		button.setLayoutData(gridData);
		label1 = new Label(parent, SWT.NONE);
		label1.setText("Busca:");
		label1.setLayoutData(gridData5);
		textArea = new Text(parent, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		textArea.setLayoutData(gridData1);
		textArea.setText("ASTPGHTIIYEAVCLHNDRTTIP");
		textArea.setFont(new Font(parent.getDisplay(),"courier new",9 , SWT.NONE));
		button1 = new Button(parent, SWT.NONE);
		button1.setText("Buscar");
		button1.setLayoutData(gridData3);
		button2 = new Button(parent, SWT.NONE);
		button2.setText("Carregar arquivo");
		button2.setLayoutData(gridData4);
		parent.setLayout(gridLayout);
		button1.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				final String retorno = textArea.getText();
				if (retorno.length() == 0)
					return;
				try{
					v = new Visualizador();

					parent.getDisplay().asyncExec(new Runnable() {
						public void run() {
							try {
								String r = Conectividade.retornarHTML(retorno);
								v.adicionarSite("NetMHC", r);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});

					parent.getDisplay().asyncExec(new Runnable() {
						public void run() {
							try {
								String d = Conectividade.retornarHTMLEpijen(retorno);
								v.adicionarSiteHTML("EpiJen", d);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
	}
}
